using System;
using System.Collections.Generic;

namespace ICSharpCode.TextEditor.Document
{
    public class HtmlFoldingStrategy : IFoldingStrategy
    {
        public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
        {
            return GenerateFoldMarkers(document);
        }

        public List<FoldMarker> GenerateFoldMarkers(IDocument document)
        {
            List<FoldMarker> list = new List<FoldMarker>();
            int num = 0;
            Stack<int> stack = new Stack<int>();
            Stack<string> stack2 = new Stack<string>();
            //int num2 = 0;
            Stack<int> stack3 = new Stack<int>();
            Stack<string> stack4 = new Stack<string>();
            checked
            {
                for (int i = 0; i < document.TotalNumberOfLines; i++)
                {
                    int textLength = document.TextLength;
                    LineSegment lineSegment = document.GetLineSegment(i);
                    int j;
                    for (j = lineSegment.Offset; j < textLength; j++)
                    {
                        char charAt;
                        if ((charAt = document.GetCharAt(j)) != ' ' && charAt != '\t')
                        {
                            break;
                        }
                    }
                    if (j == textLength)
                    {
                        break;
                    }
                    int num3 = j - lineSegment.Offset;
                    string text = document.GetText(document.GetLineSegment(i));
                    if (text.Trim().StartsWith("//region", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().StartsWith("//endregion", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text2 = stack2.Pop();
                        string foldText = text2.Replace("//region", "").Trim();
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text2.TrimStart().Length, i, num3 + text2.Length * 2 + "</endregion>".Length, FoldType.Unspecified, foldText));
                    }
                    if (text.Trim().StartsWith("<html", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().StartsWith("</html>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text3 = stack2.Pop();
                        string foldText2 = "<html>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text3.Length, i, num3 + text3.Length * 2 + "</html>".Length, FoldType.Unspecified, foldText2));
                    }
                    if (text.Trim().IndexOf("<header") == -1 && text.Trim().StartsWith("<head", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().StartsWith("</head>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text4 = stack2.Pop();
                        string foldText3 = "<head>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text4.TrimStart().Length, i, num3 + text4.Length * 2 + "</head>".Length, FoldType.Unspecified, foldText3));
                    }
                    if (text.Trim().StartsWith("<style", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<style", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</style>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text5 = stack2.Pop();
                        string foldText4 = "<style>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text5.TrimStart().Length, i, num3 + text5.Length * 2 + "</style>".Length, FoldType.Unspecified, foldText4));
                    }
                    if (text.Trim().StartsWith("<body", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().StartsWith("</body>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text6 = stack2.Pop();
                        string foldText5 = "<body>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text6.TrimStart().Length, i, num3 + text6.Length * 2 + "</body>".Length, FoldType.Unspecified, foldText5));
                    }
                    if (text.Trim().IndexOf("</form>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<form", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<form", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</form>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text7 = stack2.Pop();
                        string foldText6 = "<form>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text7.TrimStart().Length, i, num3 + text7.Length * 2 + "</form>".Length, FoldType.Unspecified, foldText6));
                    }
                    if (text.Trim().IndexOf("</table>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<table", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<table", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</table>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text8 = stack2.Pop();
                        string foldText7 = "<table>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text8.TrimStart().Length, i, num3 + text8.Length * 2 + "</table>".Length, FoldType.Unspecified, foldText7));
                    }
                    if (text.Trim().IndexOf("</tr>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<tr", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<tr", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</tr>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text9 = stack2.Pop();
                        string foldText8 = "<tr>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text9.TrimStart().Length, i, num3 + text9.Length * 2 + "</tr>".Length, FoldType.Unspecified, foldText8));
                    }
                    if (text.Trim().IndexOf("</td>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<td", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<td", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</td>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text10 = stack2.Pop();
                        string foldText9 = "<td>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text10.TrimStart().Length, i, num3 + text10.Length * 2 + "</td>".Length, FoldType.Unspecified, foldText9));
                    }
                    if (text.Trim().IndexOf("</select>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<select", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<select", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</select>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text11 = stack2.Pop();
                        string foldText10 = "<select>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text11.TrimStart().Length, i, num3 + text11.Length * 2 + "</select>".Length, FoldType.Unspecified, foldText10));
                    }
                    if (text.Trim().IndexOf("</div>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<div", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<div", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</div>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text12 = stack2.Pop();
                        string foldText11 = "<div>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text12.TrimStart().Length, i, num3 + text12.Length * 2 + "</div>".Length, FoldType.Unspecified, foldText11));
                    }
                    if (text.Trim().IndexOf("</ul>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<ul", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<ul", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</ul>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text13 = stack2.Pop();
                        string foldText12 = "<ul>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text13.TrimStart().Length, i, num3 + text13.Length * 2 + "</ul>".Length, FoldType.Unspecified, foldText12));
                    }
                    if (text.Trim().IndexOf("<link", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().IndexOf("</li>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<li", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("</link>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().IndexOf("<li", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</li>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text14 = stack2.Pop();
                        string foldText13 = "<li>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text14.TrimStart().Length, i, num3 + text14.Length * 2 + "</li>".Length, FoldType.Unspecified, foldText13));
                    }
                    if (text.Trim().IndexOf("<aside", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().IndexOf("</a>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<a", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("</aside>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().IndexOf("<a", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</a>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text15 = stack2.Pop();
                        string foldText14 = "<a>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text15.TrimStart().Length, i, num3 + text15.Length * 2 + "</a>".Length, FoldType.Unspecified, foldText14));
                    }
                    if (text.Trim().IndexOf("</section>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<section", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<section", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</section>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text16 = stack2.Pop();
                        string foldText15 = "<section>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text16.TrimStart().Length, i, num3 + text16.Length * 2 + "</section>".Length, FoldType.Unspecified, foldText15));
                    }
                    if (text.Trim().IndexOf("</header>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<header", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<header", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</header>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text17 = stack2.Pop();
                        string foldText16 = "<header>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text17.TrimStart().Length, i, num3 + text17.Length * 2 + "</header>".Length, FoldType.Unspecified, foldText16));
                    }
                    if (text.Trim().IndexOf("</footer>", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("<footer", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<footer", StringComparison.CurrentCultureIgnoreCase) == -1 && text.Trim().StartsWith("</footer>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text18 = stack2.Pop();
                        string foldText17 = "<footer>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text18.TrimStart().Length, i, num3 + text18.Length * 2 + "</footer>".Length, FoldType.Unspecified, foldText17));
                    }
                    if (text.Trim().IndexOf("</script>") == -1 && text.Trim().StartsWith("<script", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                    }
                    if (text.Trim().IndexOf("<script") == -1 && text.Trim().StartsWith("</script>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text19 = stack2.Pop();
                        string foldText18 = "<script>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text19.TrimStart().Length, i, num3 + text19.Length * 2 + "</script>".Length, FoldType.Unspecified, foldText18));
                    }
                    if ((text.Trim().StartsWith("{") || text.Trim().EndsWith("{")) && (text.Trim().StartsWith("{") || (text.Trim().IndexOf("}") == -1 && text.Trim().EndsWith("{"))))
                    {
                        stack3.Push(i);
                    }
                    if (text.Trim().StartsWith("}") && stack3.Count > 0)
                    {
                        int num4 = stack3.Pop();
                        list.Add(new FoldMarker(document, num4, document.GetLineSegment(num4).Length - "{".Length, i, 57, FoldType.TypeBody, "{...}"));
                    }
                }
                return list;
            }
        }
    }
}
